package com.yanqu.road.dataHelper.gameServer;

import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.manager.GameMgr;
import com.yanqu.road.dataHelper.manager.ServerMgr;
import com.yanqu.road.entity.player.UserPassRoad;
import com.yanqu.road.entity.servercenter.DbInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GameServer extends Base {

    ServerInfo serverInfo;

    public ServerInfo getServerInfo() {
        return serverInfo;
    }

    public GameServer(ServerInfo serverInfo){
        this.serverInfo = serverInfo;
        try {
            DbInfo dbInfo = ServerMgr.getDbInfoMap().get(serverInfo.getServerId());
            DbInfo dbLogInfo = ServerMgr.getLogDbInfoMap().get(serverInfo.getServerId());
            getLogger().info("gameConnect : {}, {}, {}", "jdbc:mysql://" + dbInfo.getIp() + ":" + dbInfo.getPort() + "/" + dbInfo.getDbName()+"?useUnicode=true&character_set_server=utf8mb4&useSSL=false", dbInfo.getUserName(), dbInfo.getPassword());
            connection =  DriverManager.getConnection("jdbc:mysql://" + dbInfo.getIp() + ":" + dbInfo.getPort() + "/" + dbInfo.getDbName()+"?useUnicode=true&character_set_server=utf8mb4&useSSL=false", dbInfo.getUserName(), dbInfo.getPassword());
            logConnection =  DriverManager.getConnection("jdbc:mysql://" + dbLogInfo.getIp() + ":" + dbLogInfo.getPort() + "/" + dbLogInfo.getDbName(), dbLogInfo.getUserName(), dbLogInfo.getPassword());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("连接失败！");
        }
    }

    public void test(long a,String b){
        System.out.println(a);
        System.out.println(b);
        try {
            ResultSet rs = null;
            CallableStatement call = null;
            call = connection.prepareCall("select * from t_u_user limit 1");
            rs = call.executeQuery();
            while (rs.next()) {
                long userId = rs.getLong("userId");
                System.out.println("userId : " + userId);
            }
        } catch (SQLException e) {
            logger.error("异常错误",e);
        }
    }

    //获取所有充值订单
    public void rechargeOrder(long a,String b){
        List<Map<String,String>> data = new ArrayList<>();
        try {
            ResultSet rs = null;
            CallableStatement call = null;
            call = connection.prepareCall("select * from t_u_recharge_detail");
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("订单编号",checkNullToEmpty(rs.getString("orderNo")));
                temp.put("用户id",checkNullToEmpty(rs.getString("userId")));
                temp.put("金额",checkNullToEmpty(rs.getString("amount")));
                temp.put("备注",checkNullToEmpty(rs.getString("remark")));
                temp.put("结果码",checkNullToEmpty(rs.getString("resultCode")));
                temp.put("结果消息",checkNullToEmpty(rs.getString("resultMsg")));
                temp.put("订单状态",checkNullToEmpty(rs.getString("orderStatus")));
                temp.put("更新时间",checkNullToEmpty(rs.getString("updateTime")));
                temp.put("平台订单号",checkNullToEmpty(rs.getString("platformOrderNo")));
                temp.put("商品id",checkNullToEmpty(rs.getString("mallId")));
                temp.put("支付类型",checkNullToEmpty(rs.getString("payType")));
                temp.put("预支付类型",checkNullToEmpty(rs.getString("preChargeType")));
                temp.put("活动id",checkNullToEmpty(rs.getString("activityId")));
                temp.put("条件id",checkNullToEmpty(rs.getString("conditionId")));
                data.add(temp);
            }
        } catch (SQLException e) {
            logger.error("异常错误",e);
        }
        DataMgr.addData(data);
    }

    //修复玩家关卡，回滚玩家的关卡
    public void repairPass(long a,String b){
        Map<String, Map<String,String>> excelData = GameMgr.getExcelData("pass");
        Map<Long,UserPassRoad> dataMap = new ConcurrentHashMap<>();
        try {
            ResultSet rs = null;
            CallableStatement call = null;
            call = connection.prepareCall("select * from t_u_pass_road order by passId DESC limit 2");
            rs = call.executeQuery();
            while (rs.next()) {
                UserPassRoad temp = new UserPassRoad();
                temp.setUserId(rs.getLong("userId"));
                temp.setPassId(rs.getInt("passId"));
                dataMap.put(temp.getUserId(),temp);
            }
        } catch (SQLException e) {
            logger.error("异常错误",e);
        }
        String sqlUpdate = "UPDATE t_u_pass_road set passId=?,passProgress=0 WHERE userId=?";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for(Long userId : dataMap.keySet()){
            int uPassId = Integer.valueOf(excelData.get(Integer.valueOf(dataMap.get(userId).getPassId()).toString()).get("uPassId"));
            logger.info(userId+"-from-"+dataMap.get(userId).getPassId()+"to"+uPassId);
            int i = 1;
            Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
            para.put(i++, new DbParameter(Types.INTEGER, uPassId));
            para.put(i++, new DbParameter(Types.BIGINT, userId));
            mapList.add(para);
        }
        if (executeBatchQuery(sqlUpdate,mapList)> -1) {
            logger.info(serverInfo.getServerId()+"处理成功");
        }
    }
}
